package com.huika.dealontop.control.myinfo.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.cropper.CropImageView;
import com.huika.dealontop.R;
import com.huika.dealontop.control.base.extend.BaseActionBarActivity;
import com.huika.dealontop.utils.UIUtils;
import com.huika.sharpsdklib.Utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Author: Victor Wu
 * Email: victor-wu*foxmail.com
 * Time: 2015/12/3 13:50
 * Description: 图片剪切
 */
public class CropImageActivity extends BaseActionBarActivity implements
        View.OnClickListener {
    //截取图片保存的路径
    public static final String RETURN_SAVE_PATH = "return_save_path";
    //
    public static final String SRC_PATH = "src_path";
    public static final String FILE_NAME = "file_name";


    private TextView tv_cancel;
    private TextView tv_rotation;
    private TextView tv_confirm;
    private CropImageView cropImageView;
    private String srcPath = "";  //需要裁剪的图片原路径
    private String bitmapName = "";   //裁剪后的图片名字



    @Override
    public void setRootView() {
        setContentView(R.layout.activity_crop_image);
        getActionBarHelper().hideActionBar();
        setResult(RESULT_CANCELED);
    }

    @Override
    protected void initData() {
        super.initData();
        if (getIntent() != null) {
            srcPath = getIntent().getStringExtra(SRC_PATH);
            bitmapName = getIntent().getStringExtra(FILE_NAME);
        }
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_rotation = (TextView) findViewById(R.id.tv_rotation);
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);
        cropImageView = (CropImageView) findViewById(R.id.CropImageView);

        tv_cancel.setOnClickListener(this);
        tv_rotation.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        cropImageView.setFixedAspectRatio(true);
        cropImageView.setImageBitmap(getBitmap(srcPath));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_confirm:
                if (cropImageView.getCroppedImage() == null) {
                    //showToastMsg("1空");
                    finish();
                } else if (getIntent().getStringExtra(FILE_NAME) == null) {
                    //showToastMsg("2空");
                    finish();
                } else {
                    saveBitmap(cropImageView.getCroppedImage(), bitmapName);
                }
                break;
            case R.id.tv_rotation:
                cropImageView.rotateImage(90);
                break;
            default:
                break;
        }
    }

    public void saveBitmap(Bitmap mBitmap, String bitName) {
        FileOutputStream m_fileOutPutStream = null;
        File file = null;
        try {
            file = new File(Utils.getCachePath(this,"CropImage") + bitName);
            if (!file.exists()) {
                file.createNewFile();
            }
            m_fileOutPutStream = new FileOutputStream(file);// 写入的文件路径
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (m_fileOutPutStream != null) {
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, m_fileOutPutStream);
        } else {
            return;
        }

        try {
            m_fileOutPutStream.flush();
            m_fileOutPutStream.close();
            Intent intent = new Intent();
            intent.putExtra(RETURN_SAVE_PATH, file.getAbsolutePath());
            setResult(RESULT_OK, intent);
            finish();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Bitmap getBitmap(String path) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(path, options);

        int width = options.outWidth;
        int hight = options.outHeight;
        int length = width > hight ? width : hight;
        int scale = length / UIUtils.getDisplayMetrics().heightPixels;
        if (scale == 0) {
            scale = 1;
        }
        options.inJustDecodeBounds = false;
        options.inSampleSize = scale;

        return BitmapFactory.decodeFile(path, options);

    }

}
